<template>
  <div class="app-container">
    <el-scrollbar style="height: 100;margin-bottom: 20px;">
      <div class="couponSelectTop">
        <div class="couponSelect" v-for="(item, key) in this.definitionInfo" :key="key">
          <div class="cust_item_box">
            <div class="textClass">
              <p>{{item.groupName}}</p>
            </div>
            <div v-if="item.groupDesc != null" class="textClass" style="margin-top: 10px">
              <span style="font-size:12px;color:#666;">{{item.groupDesc}}</span>
            </div>
            <div v-if="item.groupDesc == null" class="textClass" style="margin-top: 10px">
              <span style="font-size:12px;color:#666;">暂未设置简介</span>
            </div>
            <div class="textClass">
              <el-button @click="saveOperation(item.groupUid)" type="primary" style="color:#fff;margin-top:20px">立即新建</el-button>
            </div>
          </div>
        </div>

        <div class="couponSelect" v-if="this.definitionInfo.length == 0">
          <div class="cust_item_box">
            <div class="textClass">

            </div>
            <div class="textClass" style="margin-top: 10px">

            </div>
            <div class="textClass">
              <el-button @click="skipGroup()" type="primary" style="color:#fff;margin-top:20px">新建人群</el-button>
            </div>
          </div>
        </div>
      </div>
    </el-scrollbar>

    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
      <el-row>

      </el-row>
    </el-form>

    <el-table style="border-radius: 6px" border v-loading="loading" :data="CustomerOperationList" @selection-change="handleSelectionChange">

      <el-table-column label="计划名称" align="center" prop="operationName" :show-overflow-tooltip="true"  />
      <el-table-column label="计划类型" align="center" prop="definitionName" :show-overflow-tooltip="true"  />
      <el-table-column label="计划方式" align="center" prop="operationMode"  :formatter="operationModeFormat" />
      <el-table-column label="计划时间" align="center" prop="executeType">
        <template slot-scope="scope">
          <span>{{ scope.row.operationBeginTime }}</span>
          <span v-if="scope.row.operationEndTime != null">至{{ scope.row.operationEndTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="operationStatus" :formatter="operationStatusFormat"  />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" v-if="scope.row.operationStatus != '0' " icon="el-icon-zoom-in" @click="handleShowDetail(scope.row)"
            v-hasPermi="['CustomerOperation:CustomerOperation:query']">详细</el-button>
          <el-button size="mini"  type="text" v-if="scope.row.operationStatus == '0' " icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['CustomerOperation:CustomerOperation:edit']">修改</el-button>
          <el-button size="mini" type="text" v-if="scope.row.operationStatus != '1' " icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['CustomerOperation:CustomerOperation:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改客户运营对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @keyup.enter.native="submitForm">

    </el-dialog>

    <!-- 查看客户运营对话框 -->
    <el-dialog :title="viewTitle" :visible.sync="viewOpen" width="700px" append-to-body>
      <el-form ref="detailForm" label-width="100px">
        <el-row>
          <el-col :span="12" v-for="(item, index) in showData" :key="index" style="margin-top:-15px">
            <el-form-item :label="`${item.label}：`">
              <span>{{item.value}}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="handCloseDetail">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listCustomerOperation,
    getCustomerOperation,
    delCustomerOperation,
    addCustomerOperation,
    updateCustomerOperation
  } from "@/api/customer/CustomerOperation";
  import {
    getGroupInfo
  } from "@/api/customer/CustomerGroup";
  export default {
    name: "CustomerOperation",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 客户运营表格数据
        CustomerOperationList: [],
        // 弹出层标题
        title: "",
        //查看详细标题
        viewTitle: "",
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //查看详细数据
        showData: [],
        //人群名称简介
        definitionInfo:[],
        // 计划方式 (0:自动长期计划，1:手动定时计划)
        operationModeOptions: [],
        //运营状态(0:未开始,1:进行中,2:已暂停,3:异常暂停,4:已结束)
        operationStatusOptions:[],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          customerUid: null,
          platformId: null,
          operationType: null,
          definitionUid: null,
          operationTypeValue: null,
          operationName: null,
          operationMode: null,
          executeType: null,
          operationBeginTime: null,
          operationEndTime: null,
          operationTimeType: null,
          notificationChannel: null,
          notificationRuleType: null,
          notificationRule: null,
          notificationTime: null,
          abnormalRemind: null,
          remindPhone: null,
          createTime: null,
          updateTime: null,
          createUser: null,
          updateUser: null,
          dataStatus: null,
          createName: null,
          updateName: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getGroupInfo();
      this.getDicts("customer_operation_mode").then(response => {
        this.operationModeOptions = response.data;
      });
      this.getDicts("customer_operation_status").then(response => {
        this.operationStatusOptions = response.data;
      });
    },
    watch: {
      $route(to, from) {
        //from 从哪来
        //to //到哪去
        let path = from.path;
        if ("/updateOperation/updateOperation" == path) {
          //刷新页面
          this.getList();
        } else {
          let index = path.lastIndexOf("/");
          if (index > -1) {
            path = path.substr(0, index);
            if ("/updateOperation/updateOperation" == path) {
              //刷新页面
              this.queryParams.pageNum = 1
              this.getList();
            }
          }
        }
      }
    },
    methods: {
      //跳转人群页面
      skipGroup(){
         this.$router.push("/group/data");
      },
      // 获取人群信息
      getGroupInfo(){
        getGroupInfo().then(response => {
          this.definitionInfo = response.rows;
          console.log(this.definitionInfo)
        });
      },
      /** 查询客户运营列表 */
      getList() {
        this.loading = true;
        listCustomerOperation(this.queryParams).then(response => {
          response.rows.forEach(item => {
            if(item.operationBeginTime != null){
              item.operationBeginTime =  item.operationBeginTime.substring(0,10);
            }
            if( item.operationEndTime != null){
              item.operationEndTime=  item.operationEndTime.substring(0,10);
            }
          })
          this.CustomerOperationList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 计划方式
      operationModeFormat(row, column) {
        return this.selectDictLabel(this.operationModeOptions, row.operationMode);
      },
      operationStatusFormat(row, column) {
        return this.selectDictLabel(this.operationStatusOptions, row.operationStatus);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          operationUid: null,
          customerUid: null,
          platformId: null,
          operationType: null,
          definitionUid: null,
          operationTypeValue: null,
          operationName: null,
          operationMode: null,
          executeType: null,
          operationBeginTime: null,
          operationEndTime: null,
          operationTimeType: null,
          notificationChannel: null,
          notificationRuleType: null,
          notificationRule: null,
          notificationTime: null,
          abnormalRemind: null,
          remindPhone: null,
          createTime: null,
          updateTime: null,
          createUser: null,
          updateUser: null,
          dataStatus: "0",
          createName: null,
          updateName: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.operationUid)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加客户运营";
      },
      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        this.$router.push("/Operation/OperationDetail/" + row.operationUid);
      },
      handCloseDetail() {
        this.viewOpen = false
        this.showData = []
      },
      // 新建运营
      saveOperation(groupUid){
        let groupName = "人群运营";
        this.$router.push("/updateOperation/updateOperation/"+ groupUid);
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.$router.push("/updateOperation/updateOperationEdit/" + row.operationUid);
        // this.reset();
        // const operationUid = row.operationUid || this.ids
        // getCustomerOperation(operationUid).then(response => {
        //   this.form = response.data;
        //   this.open = true;
        //   this.title = "修改客户运营";
        // });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.operationUid != null) {
              updateCustomerOperation(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCustomerOperation(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const operationUids = row.operationUid || this.ids;
        this.$confirm('是否确认删除客户运营编号为"' + operationUids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCustomerOperation(operationUids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('CustomerOperation/CustomerOperation/export', {
          ...this.queryParams
        }, `CustomerOperation_CustomerOperation.xlsx`)
      }
    }
  };
</script>

<style lang="less" scoped>
  .couponSelectTop {
    width: 100%;
    height: 150px;
    margin-bottom: 10px;
    position: relative;
    white-space: nowrap;

    .couponSelect {
      display: inline-block;
      width: 350px;
      height: 100%;
      background: #FFF;
      border-radius: 6px;
      box-shadow: 5px 5px 10px 6px rgba(0,0,0,0.03);
      .cust_item_box {
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
      }
      &:not(:last-child) {
        margin-right: 20px;
      }
    }
  }
  .el-scrollbar__wrap {
    overflow-x: hidden;
  }
  .textClass {
    margin-top: 4%;
    text-align:center
  }

</style>
